package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.vx.vo.WxHomeIndexFloorLevel2Vo;
import com.cskaoyan.model.vx.vo.WxHomeIndexVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/2 16:06
 */
@Service
public class WxHomeIndexServiceImpl implements WxHomeIndexService {
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketGrouponMapper marketGrouponMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public WxHomeIndexVo indexShow() {

        MarketSystemExample marketSystemExample = new MarketSystemExample();
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(marketSystemExample);
        MarketSystem marketSystem = marketSystems.get(16);
        Integer indexBrand = Integer.parseInt(marketSystem.getKeyValue());
        //banner 全部显示
        MarketAdExample example = new MarketAdExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketAd> marketAds = marketAdMapper.selectByExample(example);
        WxHomeIndexVo wxHomeIndexVo = new WxHomeIndexVo();
        wxHomeIndexVo.setBanner(marketAds);

        //brandList
        PageHelper.startPage(1, indexBrand);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        wxHomeIndexVo.setBrandList(marketBrands);

        //channel
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(0).andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        wxHomeIndexVo.setChannel(marketCategories);

        //couponList
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.createCriteria().andTypeEqualTo((short) 0).andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        wxHomeIndexVo.setCouponList(marketCoupons);

        //grouponList
        MarketGrouponExample marketGrouponExample = new MarketGrouponExample();
        marketGrouponExample.createCriteria().andDeletedEqualTo(false);
        List<MarketGroupon> marketGroupons = marketGrouponMapper.selectByExample(marketGrouponExample);
        wxHomeIndexVo.setGrouponList(marketGroupons);

        Integer indexGoods = Integer.parseInt(marketSystems.get(9).getKeyValue());
        //hotGoodsList
        PageHelper.startPage(1, indexGoods);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        wxHomeIndexVo.setHotGoodsList(marketGoods);

        //newGoodsList
        Integer indexNewGoods = Integer.parseInt(marketSystems.get(1).getKeyValue());
        PageHelper.startPage(1, indexNewGoods);
        MarketGoodsExample marketGoodsExample1 = new MarketGoodsExample();
        marketGoodsExample1.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample1);
        wxHomeIndexVo.setNewGoodsList(marketGoods1);

        //topicList
        Integer indexTopics = Integer.parseInt(marketSystems.get(17).getKeyValue());
        PageHelper.startPage(1, indexTopics);
        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(topicExample);
        wxHomeIndexVo.setTopicList(marketTopics);


        //floorGoodsList
        List<WxHomeIndexFloorLevel2Vo> floorGoodsList = new ArrayList<>();
        PageHelper.startPage(1, 4);
        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        marketCategoryExample1.createCriteria().andPidEqualTo(0);
        List<MarketCategory> marketCategoriesLevel1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
        for (MarketCategory marketCategory : marketCategoriesLevel1) {
            WxHomeIndexFloorLevel2Vo goodsList = new WxHomeIndexFloorLevel2Vo();
            PageHelper.startPage(1, 4);
            MarketCategoryExample marketCategoryExample2 = new MarketCategoryExample();
            marketCategoryExample2.createCriteria().andPidEqualTo(marketCategory.getId());
            List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample2);
            for (MarketCategory category : marketCategories1) {
                PageHelper.startPage(1, 4);
                MarketGoodsExample marketGoodsExample2 = new MarketGoodsExample();
                marketGoodsExample2.createCriteria().andCategoryIdEqualTo(category.getId());
                List<MarketGoods> marketGoods2 = marketGoodsMapper.selectByExample(marketGoodsExample2);
                if (marketGoods2 != null && marketGoods2.size() >= 4) {
                    goodsList.setGoodsList(marketGoods2.subList(0, 4));
                    break;
                }
                if (marketGoods2 != null) {
                    goodsList.setGoodsList(marketGoods2);
                }
                if (goodsList.getGoodsList().size() >= 4) {
                    break;
                }
            }
            goodsList.setId(marketCategory.getId());
            goodsList.setName(marketCategory.getName());
            floorGoodsList.add(goodsList);
        }
        wxHomeIndexVo.setFloorGoodsList(floorGoodsList);
        return wxHomeIndexVo;
    }
}
